﻿///<reference path="../jquery-1.5.1-vsdoc.js" />

// Modal Window plugin 1.0 beta
// Author: Tomy
// Refactor Date: 2011.7.1
(function ($) {
    $.modalBox = function(url, options){
        if(typeof url == "string"){
            $("<a data-url='" + url + "' target='_blank' />").modalBox(options).click();
        }
    }

    $.fn.modalBox = function(options){
        if(typeof arguments[0] == "string"){
            return $("<a data-url='" + arguments[0] + "' />").modalBox(arguments[1]).click();
        }
        var settings = $.extend({}, $.fn.modalBox.defaults, options);
        return this.each(function(){
            var _this = this;
            var $trigger = $(this);
            var container = null;
            var urlData = $trigger.attr("href") || $trigger.attr("data-url");
            if(urlData){
                var array = urlData.split('#');
                if(array.length == 2){
                    var url = array[0].toString();
                    var subArray = array[1].split('>');
                    var $container = $("#" + $.trim(subArray[0]));
                    if($container.length > 0){
                        container = $container[0];
                        _initModelBox.call(_this, container, settings);
                        var ajaxRequest = false;
                        var frameAjax = false;
                         if(url){
                            ajaxRequest = true;
                            var targetData = $trigger.attr("target") || $trigger.attr("data-target");
                            if(targetData == "_blank"){ 
                                frameAjax = true;
                            }
                        }
                        $trigger.bind("click", function(){
                            if(ajaxRequest){
                                if (frameAjax) {
                                    var $containerInFrame;
                                    if (subArray[1]) {
                                        $containerInFrame = $(subArray[1].toString(), container);
                                    } else {
                                        $containerInFrame = $(container);
                                    }
                                    $("<iframe frameborder='0' id='__modalBoxFrame' name='__modalBoxFrame' />")
                                    .load(function () {
                                        _showModalBox.call(_this, container);
                                        $(".modalBoxClose", this.contentDocument).bind("click", function(){
                                            _closeModalBox.call(_this, container);
                                        });
                                    })
                                    .appendTo($containerInFrame.find("#__modalBoxFrame").remove().end())
                                    .attr("src", url);
                                } else {
                                    $(container)
                                        .find("#__modalBoxContainer").remove().end()
                                        .append($("<div id='__modalBoxContainer' />").load(url, function () {
                                            _showModalBox.call(_this, container);
                                        }));
                                }
                            }else{
                                _showModalBox.call(_this, container);
                            }
                            return false;
                        });

                        $(".modalBoxClose", container).bind("click", function(){
                            _closeModalBox.call(_this, container);
                        });
                    }
                }
            }
        });
    };

    $.fn.modalBox.defaults = {
        maskOpacity: 0.6,
        maskColor: "Black",
        holdMask: false,
        showMode: 0,
        closeTrigger: "modalBoxClose",
        handlers: {
            onModalBoxInit: function () { },
            onModalBoxShowing: function () { },
            onModalBoxShow: function() { },
            onModalBoxClosing: function () { },
            onModalBoxClosed: function () { },
            onResetPosition: function () { }
        }
    };

    //global handlers queue
    var _handlersQueue = {};
    //initialize global handlers queue
    $.each($.fn.modalBox.defaults.handlers, function(name, value){
        _handlersQueue[name] = [value];
    });

    //setup plugin
    $.fn.modalBox.setupPlugin = function(handlers){
        if(handlers){
            enqueueHandlers(_handlersQueue, handlers);
        }
    };

    $.modalBoxClose = $.fn.modalBoxClose = function(container){
        var $container = $(container);
        if($container.length > 0){
            _closeModalBox($container[0]);
        }
    };

    var _initModelBox = function(container, settings){
        var $container = $(container);
        if(!$container.data("__modalBoxInitialized")){
             $container.data("__modalBoxSettings", settings);
             var _mask = _onModalBoxInit.call(this, container, settings);
             if(!_mask){
                _mask = $("<div class='modal-box-mask'></div>").css({
                    "display": "none",
                    "z-index": 9999,
                    "position": "fixed",
                    "left": 0,
                    "top": 0,
                    "width": "100%",
                    "height": "100%",
                    "opacity": settings.maskOpacity,
                    "background-color": settings.maskColor
                });
            }
            $(_mask).click(function () {
                if(!settings.holdMask){
                    _closeModalBox.call(this, container);
                }
            }).appendTo("body");
            $container.css({
                "display": "none",
                "z-index": 10000,
                "position": "fixed",
                "left": "50%",
                "top": "50%",
                "opacity": settings.targetOpacity || 1
            })
            .resize(function () {
                _resetPosition.call(this, container);
            });
            $container.data("__modalBoxMask", _mask);
            $container.data("__modalBoxInitialized", true);
        }
    };
    var _showModalBox = function (container) {
        var $container = $(container);
        var modalBoxMask = $container.data("__modalBoxMask");
        var settings = $container.data("__modalBoxSettings");
        var callbackResult = triggerHandlers("onModalBoxShowing", this, [container, settings, modalBoxMask]);
        if (callbackResult == false)
            return;
        _resetPosition.call(this, container, settings);
        var _onModalBoxShow = function(){
            triggerHandlers("onModalBoxShow", this, [container, settings, modalBoxMask]);
        };
        if (settings.showMode == 0) {
            $container.fadeIn("slow", _onModalBoxShow);
        } else {
            $container.slideDown("slow", _onModalBoxShow);
        }
        $(modalBoxMask).fadeIn();
    };
    var _closeModalBox = function(container) {
        var $container = $(container);
        var modalBoxMask = $container.data("__modalBoxMask");
        var settings = $container.data("__modalBoxSettings");
        var callbackResult = triggerHandlers("onModalBoxClosing", this, [container, settings, modalBoxMask]);
        if(callbackResult == false)
            return;
        var _onModalBoxClosed = function(){
            triggerHandlers("onModalBoxClosed", this, [container, settings, modalBoxMask]);
        };
        if (settings.showMode == 0) {
            $container.fadeOut(_onModalBoxClosed);
        } else {
            $container.slideUp(_onModalBoxClosed);
        }
        $(modalBoxMask).fadeOut("slow");
    };
    var _resetPosition = function(container){
        var $container = $(container);
        var settings = $container.data("__modalBoxSettings");
        var callbackResult = triggerHandlers("onResetPosition", this, [container, settings]);
        if (callbackResult == false)
            return;
        $container.stop(false, false).css(callbackResult || {
            "margin-left": -$container.outerWidth() / 2,
            "margin-top": -$container.outerHeight() / 2
        });
    };
    var _onModalBoxInit = function (container, settings) {
        return triggerHandlers("onModalBoxInit", this, [container, settings]);
    };

    var triggerHandlers = function(name, target, args){
        var handlers = [];
        $.merge(handlers, _handlersQueue[name]);
        //handlers.push(_handlersQueue[name]);
        var handlerInSettings = args[1].handlers[name];
        if(handlerInSettings){
            handlers.push(handlerInSettings);
        }
        var callbackResult = null;
        $.each(handlers, function(){
            callbackResult = this.apply(target, args);
//            if(callbackResult == false){
//                return false;
//            }
        });
        return callbackResult;
    };
    var enqueueHandlers = function(handlersQueue, handlers){
        $.each($.fn.modalBox.defaults.handlers, 
        function(name, value){
            if($.isFunction(handlers[name])){
                handlersQueue[name].push(handlers[name]);
            }
        });
    };
})(jQuery);

(function($){
    $.fn.modalBox.setupPlugin({
        onModalBoxInit: function(container){
            attachDragBehavior(container);
        }
     });

    var defaults = {
        dragTrigger: "drag-behavior"
     };
    
    var initialized = false; 
    function attachDragBehavior(target) {
        var $target = $(target);
        if (!initialized){
            $(".drag-behavior", target).each(function(){
                var $dragArea = $(this);
                if(!$dragArea.data("__dragBehaviorInitialized")){
                    var $copy;
                    var originalMousePos = {};
                    var currentPos = {};
                    var dragging = false;
                    if ($dragArea.mousedown(function (e) {
                        dragging = true;
                        originalMousePos.left = e.clientX;
                        originalMousePos.top = e.clientY;
                        if (!$copy) {
                            $copy = $target.clone().css("opacity", 0.5);
                        }
                        $copy.css({
                            "margin-left": $target.css("margin-left"),
                            "margin-top": $target.css("margin-top")
                        }).appendTo("body");
                    }).length == 0) return;
                    $("html").mousemove(function (e) {
                        if (dragging) {
                            currentPos.left = parseInt($copy.css("margin-left").replace("px", ""));
                            currentPos.top = parseInt($copy.css("margin-top").replace("px", ""));
                            $copy.css({
                                "margin-left": currentPos.left + (e.clientX - originalMousePos.left),
                                "margin-top": currentPos.top + (e.clientY - originalMousePos.top)
                            });
                            originalMousePos.left = e.clientX;
                            originalMousePos.top = e.clientY;
                            return stopDefaultBehavior(e);
                        }
                    }).mouseup(function (e) {
                        if (dragging && $copy) {
                            $target.css({
                                "margin-left": $copy.css("margin-left"),
                                "margin-top": $copy.css("margin-top")
                            });
                            $copy.remove();
                        }
                        dragging = false;
                    });
                    $dragArea.data("__dragBehaviorInitialized", true);
                }
            });
            initialized = true;
        }
    }

    function stopDefaultBehavior(e) {
        if (e && e.preventDefault)
            e.preventDefault();
        else
            window.event.returnValue = false;
        return false;
    }
})(jQuery);

(function($){
    var defaults = {
        modalWin: true,
        width: 0,
        height: 0,
        padding: 10
    };
    $.notificationBox = function(title, message, options){
        var settings = $.extend({}, defaults, options);
        var $nofication = $('<div id="__notificationBox" class="box-pop-normalInfo" style="display:none">' + 
            '<div class="sideBarNavHeadSpecial patch-pop-normalInfo">' + title +
            '<span class="floatRight"><img class="modalBoxClose" src="/WebCenter/CommonModule/UI/images/Close.png" alt="close" style="cursor: pointer" height="16" /></span></div>'+
            '<div class="sideBarContent"><div class="notification-box-content">' + message + '</div></div></div>')
            .find(".notification-box-content")
            .css({ 
                "width": settings.width || "auto", 
                "height": settings.height || "auto",
                "padding": settings.padding 
            }).end().appendTo("body");

        $.modalBox("#__notificationBox", { 
            maskOpacity: 0,
            holdMask : settings.modalWin,
            handlers : {
                onModalBoxClosed: function(){
                    $nofication.remove();
                }
            }
        });
    }
})(jQuery);

$(document).ready(function(){
    $(".modalBox").modalBox();
});